package com.lihao.sam.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.StudentRepairsListDao;
import com.lihao.sam.dto.MaintenanceExamineStatisticsDto;
import com.lihao.sam.dto.StudentRepairsListDto;
import com.lihao.sam.entity.StudentRepairsList;
import com.lihao.sam.service.intf.StudentRepairsListService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Qinheyun
 * @Description:TODO
 */
@Service
public class StudentRepairsListImpl implements StudentRepairsListService {
    @Resource
    private StudentRepairsListDao dao;


    @Override
    public R add(StudentRepairsList studentRepairsList) {
        if (dao.add(studentRepairsList)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(StudentRepairsList studentRepairsList) {
        if (dao.update(studentRepairsList)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R del(int id) {
        if (dao.del(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageVo all(String repairsName, Integer state, int page, int limit) {
        //开启分页
        PageHelper.startPage(page,limit);
        //执行查询
        PageInfo<StudentRepairsList> pageInfo = new PageInfo<>(dao.all(repairsName,state));
        //返回查询数据和总条数
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R importData(MultipartFile file) throws IOException {
        //实现Excel文件导入  1.文件上传  2.解析Excel 3.操作数据库
        //验证文件是否为空
        if (!file.isEmpty()){
            //2.获取文件内容  流
            InputStream is = file.getInputStream();
            //3.解析Excel文件  文件不是一条数据 使用集合
            List<StudentRepairsList> list= EasyExcel.read(is, StudentRepairsList.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            //4.mybatis实现批量新增
            if (dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) throws IOException {
        //1.设置下载的文件名
        String fn="grade_"+System.currentTimeMillis()+".xlsx";
        //2.设置响应消息头 实现文件下载
        response.setHeader("content-disposition","attachment;filename="+fn);
        //3.写出要下载的内容
        EasyExcel.write(response.getOutputStream(), StudentRepairsList.class).
                sheet().doWrite(dao.selectAll());
    }

    @Override
    public PageVo selectMec(int page, int limit) {
        //开启分页
        PageHelper.startPage(page,limit);
        //执行查询
        PageInfo<MaintenanceExamineStatisticsDto> pageInfo = new PageInfo<>(dao.selectMec());
        //返回查询数据和总条数
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R querNum() {
        //1.查询
        List<StudentRepairsListDto> list = dao.selectNum();
        //2.组装结果数据
        Map<String, ArrayList> map=new HashMap<>();
        map.put("cause",new ArrayList());
        map.put("num",new ArrayList());
        for(StudentRepairsListDto ur:list){
            map.get("cause").add(ur.getCause());
            map.get("num").add(ur.getNum());
        }
        return R.ok(map);
    }

    @Override
    public void exportData2(HttpServletResponse response) throws IOException {
        //1.设置下载的文件名
        String fn="grade_"+System.currentTimeMillis()+".xlsx";
        //2.设置响应消息头 实现文件下载
        response.setHeader("content-disposition","attachment;filename="+fn);
        //3.写出要下载的内容
        EasyExcel.write(response.getOutputStream(), MaintenanceExamineStatisticsDto.class).
                sheet().doWrite(dao.selectMec());
    }
}
